home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
mac
/
Code
/
Chapter Code.exe
/
Chapter10
/
MultiViewpointsPanel.java
< prev
next >
Wrap
Text File
|
2000-09-08
|
7KB
|
223 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import custom_nodes.*;
public class MultiViewpointsPanel extends Shout3DPanel implements DeviceObserver {
int pixelStartX;
int pixelStartY;
float headingSpeed = 0.0f;
float pitchSpeed = 0.0f;
float distanceSpeed = 0.0f;
float zoomPixelFactor = 0.0f;
TargetViewpoint[] cameras = new TargetViewpoint [3];
int currentCam = 1;
int totalCams = 1;
float initialHeading;
float initialPitch;
float initialDistance;
float[] initialCenter;
float initialFieldOfView;
CameraCounter counter;
public MultiViewpointsPanel(Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"DeviceInput", null);
getRenderer().addRenderObserver(this, null);
cameras[0] = (TargetViewpoint) getCurrentBindableNode("Viewpoint");
//store initial values
initialHeading = cameras[0].heading.getValue();
initialPitch = cameras[0].pitch.getValue();
initialDistance = cameras[0].distance.getValue();
initialCenter = cameras[0].center.getValue();
initialFieldOfView = cameras[0].fieldOfView.getValue();
//set zoom factor
//based on initial distance
zoomPixelFactor = (initialDistance/10)/150;
//create CameraCounter
//and add to scene
counter = new CameraCounter();
Node[]tempArray = {counter};
getScene().addChildren(tempArray);
counter.number.setValue(currentCam);
}
protected void finalize() {
removeDeviceObserver(this,"DeviceInput");
getRenderer().removeRenderObserver(this);
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
//if mouse input
if (di instanceof MouseInput) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.UP:
headingSpeed = 0.0f;
pitchSpeed = 0.0f;
distanceSpeed = 0.0f;
return true;
case MouseInput.DRAG:
//if left button used
if(mi.button == 0) {
int pixelEndX = mi.x;
int pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
headingSpeed = dragDistanceX/70f;
pitchSpeed = dragDistanceY/70f;
return true;
}
//if right button used
if(mi.button == 1) {
int pixelEndY = mi.y;
int dragDistanceY = pixelEndY - pixelStartY;
distanceSpeed = dragDistanceY*zoomPixelFactor;
return true;
}
}//end of switch
return false;
}//end of mouse input
//if keyboard input
if (di instanceof KeyboardInput) {
KeyboardInput ki = (KeyboardInput) di;
if (ki.which == KeyboardInput.PRESS) {
if (ki.key == 'c' || ki.key == 'C') {
//create new camera and
//add it to the array
if (totalCams < 3) {
totalCams++;
currentCam = totalCams;
cameras[currentCam-1] = new TargetViewpoint(this);
//add to scene and bind
Node [] kids = {cameras[currentCam-1]};
getScene().addChildren(kids);
//set to initial values
cameras[currentCam-1].isBound.setValue(true);
cameras[currentCam-1].heading.setValue(initialHeading);
cameras[currentCam-1].pitch.setValue(initialPitch);
cameras[currentCam-1].distance.setValue(initialDistance);
cameras[currentCam-1].center.setValue(initialCenter);
cameras[currentCam-1].fieldOfView.setValue(initialFieldOfView);
//update CameraCounter display
counter.number.setValue(currentCam);
}
return true;
}
if (ki.key == '1') {
if (totalCams >= 1) {
currentCam = 1;
cameras[currentCam- 1].isBound.setValue(true);
counter.number.setValue(currentCam);
}
return true;
}
if (ki.key == '2') {
if (totalCams >= 2) {
currentCam = 2;
cameras[currentCam- 1].isBound.setValue(true);
counter.number.setValue(currentCam);
}
return true;
}
if (ki.key == '3') {
if (totalCams >= 3) {
currentCam = 3;
cameras[currentCam- 1].isBound.setValue(true);
counter.number.setValue(currentCam);
}
return true;
}
return false;
}//end of key PRESS
return false;
}//end of keyboard input
return false;
}//end of onDeviceInput()
public void onPreRender (Renderer r, Object o) {
float headingDelta = headingSpeed/getFramesPerSecond();
float temp = cameras[currentCam-1].heading.getValue() - headingDelta;
cameras[currentCam-1].heading.setValue(temp);
float pitchDelta = pitchSpeed/getFramesPerSecond();
temp = cameras[currentCam-1].pitch.getValue() - pitchDelta;
cameras[currentCam-1].pitch.setValue(temp);
float distanceDelta = distanceSpeed/getFramesPerSecond();
temp = cameras[currentCam-1].distance.getValue() + distanceDelta;
cameras[currentCam-1].distance.setValue(temp);
}//end of onPreRender()
}//end of class